package com.daaij.fourthchapter;

import com.learn._3stack.SimpleStack;
import com.learn._3stack.Stack;

public class ExpressionTree {


    public static void main(String[] args) {
        char[] expressionArray = new char[]{'a','b','c','*','+','d','e','*','f','+','g','*','+'};
        TreeNode head = buildExpressionTree(expressionArray);
        TreeTraverse.traverseWhileXian(head);

    }

    private static TreeNode buildExpressionTree(char[] expressionArray) {

        Stack<TreeNode> stack = new SimpleStack<>();
        for(char ch:expressionArray){
            if(isOperatorNumber(ch)){
                TreeNode treeNode = new TreeNode<>();
                treeNode.value = ch;
                stack.push(treeNode);
            }else if(isOperatorChar(ch)){
                TreeNode treeNode2 = stack.pop();
                TreeNode treeNode1 = stack.pop();
                TreeNode newHead = new TreeNode();
                newHead.value = ch;
                newHead.left = treeNode1;
                newHead.right = treeNode2;
                stack.push(newHead);
            }
        }
        return stack.pop();
    }

    //操作数
    private static boolean isOperatorNumber(char ch){
        if( (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) ){
            return true;
        }
        return false;
    }

    //操作符
    private static boolean isOperatorChar(char ch){
        if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){
            return true;
        }
        return false;
    }


}
